﻿(function () {
    'use strict';
    var educationsController = function ($scope, $modal, $log, $routeParams, educationsFactory, educationsInit) {
        $scope.educations = educationsInit;
        $scope.isCollapsed = true;
        $scope.editedEducation = {};
        $scope.resumes = [];
        $scope.selectedResumes = [];
        

        var openModal = function (size) {

            var modalInstance = $modal.open({
                templateUrl: '/app/admin/resume/education/educationsModal.html',
                controller: 'EducationsModalController',
                size: size,
                resolve: {
                    education: function () {
                        return $scope.editedEducation;
                    },
                    resumes: function() {
                        return $scope.resumes;
                    }
                }
            });

            modalInstance.result.then(function (education) {
                //alert(education.name);
                syncAssociatedResumes();
                updateEducation();
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        };
        
        $scope.edit = function (education) {
            $scope.editedEducation = education;
            if (!$scope.resumes.length) {
                getResumes()
                    .then(function(resumeData) {
                        $scope.resumes = initializeResumes(resumeData);
                        openModal('lg');
                    }, function(response) {
                        console.log(response);
                    });
            } else {
                openModal('lg');
            }
        };

        var initializeResumes = function(resumes) {
            for (var i = 0; i < resumes.length; i++) {
                var resume = resumes[i];
                if (resumeIsAssociated(resume)) {
                    resume.selected = true;
                } else {
                    resume.selected = false;
                }
            }
            return resumes;
        };

        var syncAssociatedResumes = function () {
            for (var i = 0; i < $scope.resumes.length; i++) {
                if ($scope.resumes[i].selected) {
                    if (!resumeIsAssociated($scope.resumes[i])) {
                        $scope.editedEducation.resumes.push($scope.resumes[i]);
                    }
                } else {
                    removeAssociatedResume($scope.resumes[i]);
                }
            }
        }

        var resumeIsAssociated = function (resume) {
            var assocResumes = $scope.editedEducation.resumes;
            if (assocResumes.length) {
                for (var i = 0; i < assocResumes.length; i++) {
                    if (assocResumes[i].name == resume.name) {
                        return true;
                    }
                }
            }
            return false;
        };

        var removeAssociatedResume = function(resume) {
            var assocResumes = $scope.editedEducation.resumes;
            if (assocResumes.length) {
                for (var i = 0; i < assocResumes.length; i++) {
                    if (assocResumes[i].name == resume.name) {
                        assocResumes.splice(i, 1);
                        break;
                    }
                }
            }
        };

        $scope.remove = function (education) {
            alert(education.name);
        };

        $scope.onCollapsed = function() {
            $scope.isCollapsed = !$scope.isCollapsed;
        };

        var getResumes = function () {
            return educationsFactory.getResumes();
                
        };
        
        $scope.getEducations = function() {
            educationsFactory.getEducations()
                .then(function(educationData) {
                    $scope.educations = educationData;
                }, function(response) {
                    console.log(response);
                });
        };

        var updateEducation = function() {
            educationsFactory.updateEducation($scope.editedEducation)
                .then(function (response) {
                    console.log(response);
                    //clearEditMode();
                }, function (response) {
                    console.log(response);
                });
        };
        
    };

    educationsController.$inject = ['$scope', '$modal', '$log', '$routeParams', 'educationsFactory', 'educationsInit'];
    angular.module('app')
        .controller('EducationsController', educationsController);
}());

(function () {
    'use strict';
    var educationsModalController = function ($scope, $modalInstance, education, resumes) {
        $scope.education = education;
        $scope.resumes = resumes;
        $scope.origResumes = angular.copy(resumes);
        $scope.origEducation = angular.copy(education);

        $scope.modalSave = function () {
            $modalInstance.close($scope.education);
        };

        $scope.modalCancel = function () {
            angular.copy($scope.origResumes, resumes);
            angular.copy($scope.origEducation, education);
            $modalInstance.dismiss('cancel');
        };
    };

    educationsModalController.$inject = ['$scope', '$modalInstance', 'education', 'resumes'];
    angular.module('app')
        .controller('EducationsModalController', educationsModalController);
}());